home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue31 / cdevents / CDEVENTS.ZIP / CDEvents.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-01-21  |  8.8 KB  |  204 lines

  1. {***************************************************************************}
  2. { [TCDEVENTS]                                                               }
  3. {   Special Units = DBTMsg                                                  }
  4. {   Version = 1.0                                                           }
  5. { [SOURCE]                                                                  }
  6. {   Copyright ⌐ 1998 by Tom Deprez                                          }
  7. {   (Just to prevent to get multiple components, which all have great       }
  8. {    things but would be a GREAT component with a lot of GREAT thing        }
  9. {    That way everybody can benefit of such a GREAT component)              }
  10. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  11. { [COMPONENT]                                                               }
  12. { [properties]                                                              }
  13. { [events]                                                                  }
  14. {   AfterArrival : Fires when new CD is inserted by user                    }
  15. {   AfterRemove  : Fires when CD is removed by user                         }
  16. {                                                                           }
  17. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  18. { [AUTHOR]                                                                  }
  19. {   Author name   = ZifNab (Tom Deprez)                                     }
  20. {   Author e-mail = tom.deprez@uz.kuleuven.ac.be                            }
  21. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  22. { [HISTORY]                                                                 }
  23. {   18/01/1998 : first release                                              }
  24. {                                                                           }
  25. {***************************************************************************}
  26.  
  27. {***************************************************************************}
  28. {  [COMMENTS]                                                               }
  29. {   I very often visit the Delphi page of the www.Experts-Exchange.com site }
  30. {   It's a place where you can ask or answer questions. One day somebody    }
  31. {   needed to know when a CD is inserted in or ejected out of the CD-Rom    }
  32. {   drive. I found it an interesting question, especially because I could   }
  33. {   not find some 'delphi' source for this problem.                         }
  34. {   I started to search for a solution.                                     }
  35. {   Well, that was the beginning of this component. Where it ends?          }
  36. {   That I don't know, it really depends on the reaction of you! If I get   }
  37. {   some reaction, I'll futher improve it, otherwise it sticks with this.   }
  38. {   Too bad, because I think it can become a great tool.                    }
  39. {                                                                           }
  40. {  [IMPORTANT]                                                              }
  41. {    Made some improvements? : please let me know of, so I can update it    }
  42. {    Need some improvements? : please, just ask and I'll try to make it     }
  43. {    Have you got some ideas? : please, send them to me                     }
  44. {    You use this component? : please, send me an e-mail, why you use this  }
  45. {                              component. An E-mail isn't asked too much.   }
  46. {                              Isn't it? At least let me know of the fact   }
  47. {                              you're using it. It makes me very HAPPY!     }
  48. {  [THANKS]                                                                 }
  49. {    Matvey who brought the first idea to this component                    }
  50. {    Perhaps YOU?                                                           }
  51. {                                                                           }
  52. { ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ }
  53. {  [COPYRIGHT]                                                              }
  54. {    This file is distributed as freeware and without warranties of any     }
  55. {    kind. You can use it in your own applications at your own risk.        }
  56. {                                                                           }
  57. {  [NOTE]                                                                   }
  58. {    It's freeware, but don't hesitate to send me some money if you've      }
  59. {    become rich with the help of this component ;-)                        }
  60. {    I's freeware, but don't hesitate to send me the program or the         }
  61. {    component, where you use this component in. I would be ever gratefull  }
  62. {    to you, thanks.                                                        }
  63. {***************************************************************************}
  64.  
  65. unit CDEvents;
  66.  
  67. interface
  68.  
  69. uses
  70.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  71.   DBTMsg, DsgnIntf;
  72.  
  73. const cVersion = '1.0';
  74.  
  75. type
  76.  TDeviceChangeEvent = procedure (Sender : TObject; FirstDriveLetter : char)
  77.                          of object;
  78.  
  79.  TAboutZifNabProperty = class(TPropertyEditor)
  80.   public
  81.     procedure Edit; override;
  82.     function GetAttributes: TPropertyAttributes; override;
  83.     function GetValue: string; override;
  84.   end;
  85.  
  86.  TCDEvents = class(TComponent)
  87.   private
  88.     { Private declarations }
  89.      FAbout : TAboutZifNabProperty;
  90.      FWindowHandle: HWND;
  91.      fAfterArrival: TDeviceChangeEvent;
  92.      fAfterRemove: TDeviceChangeEvent;
  93.    procedure WndProc(var Msg: TMessage);
  94.   protected
  95.     { Protected declarations }
  96.      function GetFirstDriveLetter(unitmask:longint):char;
  97.      procedure WMDeviceChange(var Msg : TWMDeviceChange); dynamic;
  98.   public
  99.     { Public declarations }
  100.      constructor Create(AOwner: TComponent); override;
  101.      destructor Destroy; override;
  102.   published
  103.     { Published declarations }
  104.     property About : TAboutZifNabProperty read FAbout write FAbout;
  105.     property AfterArrival : TDeviceChangeEvent read fAfterArrival write fAfterArrival;
  106.     property AfterRemove: TDeviceChangeEvent read fAfterRemove write fAfterRemove;
  107.   end;
  108.  
  109. procedure Register;
  110.  
  111. implementation
  112.  
  113. {*********************** tAboutZifNabproperty component ***********************}
  114.  
  115. procedure TAboutZifNabProperty.Edit;
  116. begin
  117.   Application.MessageBox ('        tCDEvents v'+cVersion
  118.                          +#13#10'This component is freeware.'
  119.                          +#13#10'     ⌐ 1998 ZifNab         '
  120.                          +#13#10''
  121.                          +#13#10'mailto:Tom.Deprez@uz.kuleuven.ac.be',
  122.                          'About',
  123.                          MB_OK+ MB_ICONINFORMATION);
  124. end;
  125.  
  126. function TAboutZifNabProperty.GetAttributes: TPropertyAttributes;
  127. begin
  128.   Result := [paMultiSelect, paDialog, paReadOnly];
  129. end;
  130.  
  131. function TAboutZifNabProperty.GetValue: string;
  132. begin
  133.   Result := '(about)';
  134. end;
  135.  
  136. {**************************** tCDEvents component *****************************}
  137.  
  138. constructor TCDEvents.Create(AOwner: TComponent);
  139. begin
  140.   inherited Create(AOwner);
  141.   FWindowHandle := AllocateHWnd(WndProc);
  142. end;
  143.  
  144. destructor TCDEvents.Destroy;
  145. begin
  146.   DeallocateHWnd(FWindowHandle);
  147.   inherited Destroy;
  148. end;
  149.  
  150. procedure TCDEvents.WndProc(var Msg: TMessage);
  151. begin
  152.      if (Msg.Msg = WM_DEVICECHANGE) then
  153.       try
  154.         WMDeviceChange(TWMDeviceChange(Msg));
  155.       except
  156.         Application.HandleException(Self);
  157.       end
  158.     else
  159.       Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
  160. end;
  161.  
  162. function TCDEvents.GetFirstDriveLetter(unitmask : longint):char;
  163. var DriveLetter : shortint;
  164. begin
  165.  DriveLetter := Ord('A');
  166.  while (unitmask and 1)=0  do begin
  167.   unitmask := unitmask shr 1;
  168.   inc(DriveLetter);
  169.  end;
  170.  Result := Char(DriveLetter);
  171. end;
  172.  
  173. procedure TCDEvents.WMDeviceChange(var Msg : TWMDeviceChange);
  174. var lpdb : PDEV_BROADCAST_HDR;
  175.     lpdbv : PDEV_BROADCAST_VOLUME;
  176. begin
  177.  (* received a wm_devicechange message *)
  178.   lpdb := PDEV_BROADCAST_HDR(Msg.dwData);
  179.    if lpdb^.dbch_devicetype = DBT_DEVTYP_VOLUME then begin
  180.     (* message comes from a logical volume *)
  181.     lpdbv := PDEV_BROADCAST_VOLUME(Msg.dwData);
  182.     if (lpdbv^.dbcv_flags and DBTF_MEDIA) = 1 then begin
  183.      (* Event's media flag is set *)
  184.       case Msg.Event of
  185.        DBT_DEVICEARRIVAL : if Assigned(fAfterArrival) then
  186.                    fAfterArrival(Self,
  187.                                  GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
  188.        DBT_DEVICEREMOVECOMPLETE : if Assigned(fAfterRemove) then
  189.                    fAfterRemove(Self,
  190.                                 GetFirstDriveLetter(lpdbv^.dbcv_unitmask));
  191.       end;
  192.     end;
  193.    end;
  194. end;
  195.  
  196. procedure Register;
  197. begin
  198.   RegisterComponents('Samples', [TCDEvents]);
  199.   RegisterPropertyEditor(TypeInfo(TAboutZifNabProperty), TCDEvents,
  200.                          'ABOUT', TAboutZifNabProperty);
  201. end;
  202.  
  203. end.
  204.